﻿
namespace AdventureWorksLT.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using AdventureWorksLT.Web.Models;


    // Implements application logic using the AdventureWorksLTEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class AdventureWorksLTDomainService : LinqToEntitiesDomainService<AdventureWorksLTEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Addresses' query.
        public IQueryable<Address> GetAddresses()
        {
            return this.ObjectContext.Addresses;
        }

        public void InsertAddress(Address address)
        {
            if ((address.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(address, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Addresses.AddObject(address);
            }
        }

        public void UpdateAddress(Address currentAddress)
        {
            this.ObjectContext.Addresses.AttachAsModified(currentAddress, this.ChangeSet.GetOriginal(currentAddress));
        }

        public void DeleteAddress(Address address)
        {
            if ((address.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(address, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Addresses.Attach(address);
                this.ObjectContext.Addresses.DeleteObject(address);
            }
        }

       

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Customers' query.

        //[Query(IsDefault = true, ResultLimit = 100)]
        public IQueryable<Customer> GetCustomers()
        {
            return this.ObjectContext.Customers;
        }

        public IQueryable<Customer> GetCustomersForCustomer(int customerId)
        {
            return this.ObjectContext.Customers.Where(p => p.CustomerID == customerId);
        }

        public void InsertCustomer(Customer customer)
        {
            if ((customer.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Customers.AddObject(customer);
            }
        }

        public void UpdateCustomer(Customer currentCustomer)
        {
            this.ObjectContext.Customers.AttachAsModified(currentCustomer, this.ChangeSet.GetOriginal(currentCustomer));
        }

        public void DeleteCustomer(Customer customer)
        {
            if ((customer.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Customers.Attach(customer);
                this.ObjectContext.Customers.DeleteObject(customer);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'CustomerAddresses' query.
        public IQueryable<CustomerAddress> GetCustomerAddresses()
        {
            return this.ObjectContext.CustomerAddresses;
        }

        public void InsertCustomerAddress(CustomerAddress customerAddress)
        {
            if ((customerAddress.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(customerAddress, EntityState.Added);
            }
            else
            {
                this.ObjectContext.CustomerAddresses.AddObject(customerAddress);
            }
        }

        public void UpdateCustomerAddress(CustomerAddress currentCustomerAddress)
        {
            this.ObjectContext.CustomerAddresses.AttachAsModified(currentCustomerAddress, this.ChangeSet.GetOriginal(currentCustomerAddress));
        }

        public void DeleteCustomerAddress(CustomerAddress customerAddress)
        {
            if ((customerAddress.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(customerAddress, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.CustomerAddresses.Attach(customerAddress);
                this.ObjectContext.CustomerAddresses.DeleteObject(customerAddress);
            }
        }

    

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Products' query.
        public IQueryable<Product> GetProducts()
        {
            return this.ObjectContext.Products;
        }

        public void InsertProduct(Product product)
        {
            if ((product.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(product, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Products.AddObject(product);
            }
        }

        public void UpdateProduct(Product currentProduct)
        {
            this.ObjectContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct));
        }

        public void DeleteProduct(Product product)
        {
            if ((product.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(product, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Products.Attach(product);
                this.ObjectContext.Products.DeleteObject(product);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ProductCategories' query.
        public IQueryable<ProductCategory> GetProductCategories()
        {
            return this.ObjectContext.ProductCategories;
        }

        public void InsertProductCategory(ProductCategory productCategory)
        {
            if ((productCategory.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(productCategory, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ProductCategories.AddObject(productCategory);
            }
        }

        public void UpdateProductCategory(ProductCategory currentProductCategory)
        {
            this.ObjectContext.ProductCategories.AttachAsModified(currentProductCategory, this.ChangeSet.GetOriginal(currentProductCategory));
        }

        public void DeleteProductCategory(ProductCategory productCategory)
        {
            if ((productCategory.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(productCategory, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ProductCategories.Attach(productCategory);
                this.ObjectContext.ProductCategories.DeleteObject(productCategory);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ProductDescriptions' query.
        public IQueryable<ProductDescription> GetProductDescriptions()
        {
            return this.ObjectContext.ProductDescriptions;
        }

        public void InsertProductDescription(ProductDescription productDescription)
        {
            if ((productDescription.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(productDescription, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ProductDescriptions.AddObject(productDescription);
            }
        }

        public void UpdateProductDescription(ProductDescription currentProductDescription)
        {
            this.ObjectContext.ProductDescriptions.AttachAsModified(currentProductDescription, this.ChangeSet.GetOriginal(currentProductDescription));
        }

        public void DeleteProductDescription(ProductDescription productDescription)
        {
            if ((productDescription.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(productDescription, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ProductDescriptions.Attach(productDescription);
                this.ObjectContext.ProductDescriptions.DeleteObject(productDescription);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ProductModels' query.
        public IQueryable<ProductModel> GetProductModels()
        {
            return this.ObjectContext.ProductModels;
        }

        public void InsertProductModel(ProductModel productModel)
        {
            if ((productModel.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(productModel, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ProductModels.AddObject(productModel);
            }
        }

        public void UpdateProductModel(ProductModel currentProductModel)
        {
            this.ObjectContext.ProductModels.AttachAsModified(currentProductModel, this.ChangeSet.GetOriginal(currentProductModel));
        }

        public void DeleteProductModel(ProductModel productModel)
        {
            if ((productModel.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(productModel, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ProductModels.Attach(productModel);
                this.ObjectContext.ProductModels.DeleteObject(productModel);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ProductModelProductDescriptions' query.
        public IQueryable<ProductModelProductDescription> GetProductModelProductDescriptions()
        {
            return this.ObjectContext.ProductModelProductDescriptions;
        }

        public void InsertProductModelProductDescription(ProductModelProductDescription productModelProductDescription)
        {
            if ((productModelProductDescription.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(productModelProductDescription, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ProductModelProductDescriptions.AddObject(productModelProductDescription);
            }
        }

        public void UpdateProductModelProductDescription(ProductModelProductDescription currentProductModelProductDescription)
        {
            this.ObjectContext.ProductModelProductDescriptions.AttachAsModified(currentProductModelProductDescription, this.ChangeSet.GetOriginal(currentProductModelProductDescription));
        }

        public void DeleteProductModelProductDescription(ProductModelProductDescription productModelProductDescription)
        {
            if ((productModelProductDescription.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(productModelProductDescription, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ProductModelProductDescriptions.Attach(productModelProductDescription);
                this.ObjectContext.ProductModelProductDescriptions.DeleteObject(productModelProductDescription);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'SalesOrderDetails' query.
        public IQueryable<SalesOrderDetail> GetSalesOrderDetails()
        {
            return this.ObjectContext.SalesOrderDetails;
        }

        public IQueryable<SalesOrderDetail> GetSalesOrderDetailsForOrder(int orderID)
        {
            return this.ObjectContext.SalesOrderDetails.Where(p => p.SalesOrderID == orderID);
        }

        public void InsertSalesOrderDetail(SalesOrderDetail salesOrderDetail)
        {
            if ((salesOrderDetail.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(salesOrderDetail, EntityState.Added);
            }
            else
            {
                this.ObjectContext.SalesOrderDetails.AddObject(salesOrderDetail);
            }
        }

        public void UpdateSalesOrderDetail(SalesOrderDetail currentSalesOrderDetail)
        {
            this.ObjectContext.SalesOrderDetails.AttachAsModified(currentSalesOrderDetail, this.ChangeSet.GetOriginal(currentSalesOrderDetail));
        }

        public void DeleteSalesOrderDetail(SalesOrderDetail salesOrderDetail)
        {
            if ((salesOrderDetail.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(salesOrderDetail, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.SalesOrderDetails.Attach(salesOrderDetail);
                this.ObjectContext.SalesOrderDetails.DeleteObject(salesOrderDetail);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'SalesOrderHeaders' query.
        public IQueryable<SalesOrderHeader> GetSalesOrderHeaders()
        {
            return this.ObjectContext.SalesOrderHeaders;
        }

        public IQueryable<SalesOrderHeader> GetSalesOrderHeadersForCustomer(int customerId)
        {
            return this.ObjectContext.SalesOrderHeaders.Where(p => p.CustomerID == customerId);
        }

        public IQueryable<SalesOrderHeader> GetSalesOrderHeadersForSaleOrderNumber(int saleOrderID)
        {
            return this.ObjectContext.SalesOrderHeaders.Where(p => p.SalesOrderID == saleOrderID);
        }

        public void InsertSalesOrderHeader(SalesOrderHeader salesOrderHeader)
        {
            if ((salesOrderHeader.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(salesOrderHeader, EntityState.Added);
            }
            else
            {
                this.ObjectContext.SalesOrderHeaders.AddObject(salesOrderHeader);
            }
        }

        public void UpdateSalesOrderHeader(SalesOrderHeader currentSalesOrderHeader)
        {
            this.ObjectContext.SalesOrderHeaders.AttachAsModified(currentSalesOrderHeader, this.ChangeSet.GetOriginal(currentSalesOrderHeader));
        }

        public void DeleteSalesOrderHeader(SalesOrderHeader salesOrderHeader)
        {
            if ((salesOrderHeader.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(salesOrderHeader, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.SalesOrderHeaders.Attach(salesOrderHeader);
                this.ObjectContext.SalesOrderHeaders.DeleteObject(salesOrderHeader);
            }
        }
    }
}


